package puppy.ML;

import java.util.Collections;

import java.util.Hashtable;

import java.util.LinkedList;

import java.util.Vector;

import util.io.FileInput;
import util.nlp.HashDictionary;
import util.parser.GenericEntryParser;

public class CollectionGenerator {

	/**
	 * @param args
	 */

	/**
	 * Generate collection with wikititles,
	 */
	private HashDictionary redirects = null;

	private HashDictionary id = null;

	private HashDictionary wiki_types = null;

	private Hashtable<String, Integer> frequency = new Hashtable<String, Integer>();

	public CollectionGenerator(String wikis_expanded, String redirects_file,
			String types) {

		int values[] = { 1 };
		// id = new HashDictionary(wikis_expanded, "\"", "\t", 0, values);

		redirects = new HashDictionary(redirects_file, "", "\t", 0, values);
		wiki_types = new HashDictionary(types, "", "\t", 0, values);

	}

	public void generateCollection(String wikis_expanded, boolean level) {

		FileInput in = new FileInput(wikis_expanded);
		Hashtable<Integer, Integer> hash = new Hashtable<Integer, Integer>();

		String line = in.readString();

		while (line != null) {

			String t[] = GenericEntryParser.parseEntry(line, "\t", "\"");

			Integer id = Integer.valueOf(t[0]);

			// first level categories

			LinkedList<String> cats = new LinkedList<String>();

			int lev = -1;

			if (!t[2].equals("")) {
				cats.add(t[2]);

				if (level)
					lev = 1;

				addCategories(t[2], hash, lev);

			}

			// first level template

			if (!t[3].equals("")) {

				addCategories(t[3], hash, lev);

			}

			// expanded categories

			for (int i = 6; i <= 8; i++) {
				if (lev > 0)
					lev = lev + 1;

				addCategories(t[i], hash, lev);
			}

			// expanded templates
			if (lev > 0)
				lev = 0;

			for (int i = 9; i <= 12; i++) {
				if (lev >= 0)
					lev = lev + 1;

				addCategories(t[i], hash, lev);

			}

			int value = -1;
			String features = getLearnerFeatures(id, hash, level, value);
			System.out.println(features);
			line = null;
			features = null;
			line = in.readString();
		}

	}

	public void printCategoriesWithClass(String file) {

		FileInput in = new FileInput(file);

		String line = in.readString();
		LinkedList<String> type = null;

		while (line != null) {
			// System.out.println(line);
			String t[] = GenericEntryParser.parseEntry(line, "\t", "\"");
			// System.out.println(line);
			if (t != null && t.length > 1) {
				if (wiki_types.containsKey(t[1])) {
					type = wiki_types.getEntries(t[1]);
				} else {
					if (redirects.containsKey(t[1])) {
						String redirect = redirects.getEntries(t[1]).get(0);
						if (wiki_types.containsKey(redirect)) {
							type = wiki_types.getEntries(redirect);
						}
					}
				}

				if (type != null) {

					for (int i = 0; i < type.size(); i++) {
						System.out.println("\"" + type.get(i) + "\"\t" + line);

					}

				} else {

				}
				System.out.println("\"" + "null" + "\"\t" + line);

			}
			t = null;
			type = null;
			line = in.readString();
		}

	}
	

	public String getLearnerFeatures(int id, Hashtable<Integer, Integer> hash,
			boolean level, int value) {

		String features = String.valueOf(value);

		Vector v = new Vector(hash.keySet());
		Collections.sort(v);

		for (int i = 0; i < v.size(); i++) {

			features = features + " " + v.get(i) + ":" + (double) 1
					/ (double) hash.get(v.get(i));

		}

		features = features + "  # " + id;

		return features;

	}

	public void addCategories(String t, Hashtable<Integer, Integer> hash,
			int level) {

		if (t == null)
			return;

		if (t.equals(""))
			return;

		String cats[] = t.split("\t");

		for (int i = 0; i < cats.length; i++) {

			/*
			 * Update hash with frequency of categories / templates
			 */

			if (frequency.containsKey(cats[i])) {

				frequency.put(cats[i], frequency.get(cats[i]) + 1);
			} else {

				frequency.put(cats[i], 1);

			}

			if (id.containsKey(cats[i])) {

				LinkedList<String> ids = id.getEntries(cats[i]);

				Integer id_cat = Integer.getInteger(ids.get(0));
				if (hash.containsKey(id_cat)) {

					if (level > 0) {
						int current_level = hash.get(id_cat);

						if (current_level > level) {
							hash.put(id_cat, level);
						}

					} else {

						hash.put(id_cat, hash.get(id_cat) + 1);
					}

				} else {
					if (level > 0) {

						hash.put(id_cat, level);

					} else {

						hash.put(id_cat, new Integer(1));
					}

				}

			}
		}

	}

	public void test(String bigFile) {

		FileInput file = new FileInput(bigFile);

		String line = file.readString();

		Hashtable<String, LinkedList<String>> hash = new Hashtable<String, LinkedList<String>>();
		while (line != null) {

			System.out.println(line);
			LinkedList<String> list = new LinkedList<String>();

			String t[] = GenericEntryParser.parseEntry(line, "\t", "\"");
			list.add(t[1]);
			hash.put(t[0], list);
			line = file.readString();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String path = "/media/sata/data/wikipedia/text/categories_teplates_expanded1.txt";
		path = "/media/sata/data/wikipedia/text/wiki_ids1.txt";

		String redirects = "/media/sata/data/wikipedia/en/redirects_en.txt";
		String classes = "/media/sata/data/wikipedia/en/dbpedia_classes.txt";
		CollectionGenerator col = new CollectionGenerator(path, redirects,
				classes);

		path = "/media/sata/data/wikipedia/text/categories_teplates_expanded1.txt";
		col.printCategoriesWithClass(path);

		int values[] = { 1 };
		// col.hashDictionary(path, "\"", "\t", 0, values);
		// col.test(path);
		// col.generateCollection(path, false);

	}

}
